<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="name">
      <single-upload v-model="dataForm.imageUrl"></single-upload>
    </el-form-item>
    <el-form-item label="链接" prop="url">
      <el-input v-model="dataForm.url" placeholder="链接"></el-input>
    </el-form-item>
    <el-form-item label="商城类型" prop="type">
      <el-select v-model="dataForm.type" placeholder="请选择商城类型">
         <el-option
           v-for="item in mallTypeList"
           :key="item.itemValue"
           :label="item.itemText"
           :value="item.itemValue">
         </el-option>
       </el-select>
    </el-form-item>
    <el-form-item label="上线状态" prop="status">
      <el-switch v-model="dataForm.status"
        active-text="上线"
        inactive-text="下线"
       :active-value="1"
       :inactive-value="0"
       active-color="#13ce66" inactive-color="#ff4949"></el-switch>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import singleUpload from "@/components/upload/singleUpload"
  import {
    getUUID
  } from '@/utils'
  export default {
    components: {
      singleUpload
    },
    data () {
      return {
        visible: false,
        mallTypeList: [],
        dataForm: {
          id: 0,
          name: '',
          url: '',
          price: '',
          type: '',
          createTime: '',
          status: '',
          imageUrl: ''
        },
        dataRule: {
          name: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '链接不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '1 淘宝 2 京东 3 拼多多不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态 0下 1上不能为空', trigger: 'blur' }
          ]
        }
      }
    },

    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$http({
          url: this.$http.adornUrl('/sys/sysdicitem/getByCode'),
          method: 'get',
          params: this.$http.adornParams({
           'dictCode': 'mall_type'
         })
        }).then(({data}) => {
          this.mallTypeList = data.data
        })
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/goods/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.goods.name
                this.dataForm.url = data.goods.url
                this.dataForm.price = data.goods.price
                this.dataForm.type = data.goods.type
                this.dataForm.createTime = data.goods.createTime
                this.dataForm.status = data.goods.status
                this.dataForm.imageUrl = data.goods.imageUrl
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/goods/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name+getUUID(),
                'url': this.dataForm.url,
                'price': this.dataForm.price,
                'type': this.dataForm.type,
                'createTime': this.dataForm.createTime,
                'status': this.dataForm.status,
                'imageUrl': this.dataForm.imageUrl
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
